<template>
  <van-icon
    :color="value === 1 ? 'red' : ''"
    :name="value === 1 ? 'good-job' : 'good-job-o'"
    @click="onLike"
  />
</template>

<script>
import { addLike, deleteLike } from '@/api/article'
export default {
  props: {
    value: {
      type: Number,
      required: true
    },
    autId: {
      type: String,
      required: true
    }
  },
  methods: {
    async onLike () {
      this.$toast.loading({
        duration: 0,
        message: '操作中...',
        forbidClick: true
      })

      try {
        if (this.value === 1) {
          await deleteLike(this.autId)
          this.$emit('input', 0)
          this.$toast.success('取消点赞')
        } else {
          await addLike(this.autId)
          this.$emit('input', 1)
          this.$toast.success('点赞成功')
        }
      } catch (err) {
        this.$toast.fail('操作失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
